package com.migo.api;

import com.alibaba.fastjson.JSONArray;
import com.migo.annotation.IgnoreAuth;
import com.migo.defence.RtCode;
import com.migo.entity.MgHomeVideoEntity;
import com.migo.entity.MgSampleVideoEntity;
import com.migo.service.MgHomeVideoService;
import com.migo.service.MgSampleVideoService;
import com.migo.utils.JsonUtils;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import com.migo.validator.Assert;
import com.migo.view.MgHomeVideoView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * PVMG10-APP首页查询视频
 */
@RestController
@RequestMapping("/api")
public class ApiHomeVideoController {
    @Autowired
    private MgHomeVideoService mgHomeVideoService;
    @Autowired
    private MgSampleVideoService mgSampleVideoService;

    @Autowired
    private RedisCacheUtil<List<MgSampleVideoEntity>> redisCacheUtil;

    /**
     * 首页查询视频清单
     */
    @IgnoreAuth
    @RequestMapping(value = "queryHomeVideo", produces = "application/json;charset=utf-8;")
    public R queryHomeVideo(@RequestParam Map<String, Object> params) {
        String language = "en";
        if (params.containsKey("language")) {
            language = (String) params.get("language");
        } else {
            params.put("language", "en");
        }
        List<MgSampleVideoEntity> mgSampleVideoList = null;
        try {
            String key = "queryHomeVideo_" + params.get("language");
            if (redisCacheUtil.hashKey(key)) {
                mgSampleVideoList = redisCacheUtil.getCacheList(key);
                mgSampleVideoList.sort(Comparator.comparing(MgSampleVideoEntity::getSort));
                if (params.containsKey("flush")) {  //刷新标志
                    redisCacheUtil.deleteObject(key);
                }
            } else {
                //查询数据库写入redis
                synchronized (this) {
                    mgSampleVideoList = mgSampleVideoService.querySampleVideoList(params);
                    redisCacheUtil.setCacheList(key, mgSampleVideoList);
                }
            }
            return R.ok().put("data", mgSampleVideoList);
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
    }


    public List<MgHomeVideoView> coverView(List<MgHomeVideoEntity> list) {
        List<MgHomeVideoView> views = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MgHomeVideoEntity entity = list.get(i);
            MgHomeVideoView view = new MgHomeVideoView();
            BeanUtils.copyProperties(view, entity);
            views.add(view);
        }
        return views;
    }

    /**
     * 首页查询视频清单
     *
     */
    @IgnoreAuth
    @RequestMapping(value = "queryHomePage", produces = "application/json;charset=utf-8;")
    public R queryHomePage(@RequestParam Map<String, Object> params) {
        try{
            return R.ok().put("data",mgSampleVideoService.queryHomePage(params));
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
    }

    /**
     * 视频相信内容（包括功能点索引）
     *
     */
    @IgnoreAuth
    @RequestMapping(value = "queryTutorialVideoDetail", produces = "application/json;charset=utf-8;")
    public R queryTutorialVideoDetail(@RequestParam Map<String, Object> params) {
        try{
            return R.ok().put("data",mgSampleVideoService.queryTutorialVideoDetail(params));
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
    }

    /**
     * 分页获取分栏视频
     *
     */
    @IgnoreAuth
    @RequestMapping(value = "queryTutorialVideoByPage", produces = "application/json;charset=utf-8;")
    public R queryTutorialVideoByPage(@RequestParam Map<String, Object> params) {
        try{
            return R.ok().put("data",mgSampleVideoService.queryTutorialVideoByPage(params));
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
    }
}
